<!DOCTYPE html>
<html >

<head>

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Chapter 3 统计量 | ggplot2忍者秘笈</title>
  <meta name="description" content="Chapter 3 统计量 | ggplot2忍者秘笈">
  <meta name="generator" content="bookdown  and GitBook 2.6.7">

  <meta property="og:title" content="Chapter 3 统计量 | ggplot2忍者秘笈" />
  <meta property="og:type" content="book" />
  
  
  
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="Chapter 3 统计量 | ggplot2忍者秘笈" />
  
  
  

<meta name="author" content="余光创">



  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  
  
<link rel="prev" href="chapter-facet.html">
<link rel="next" href="references.html">
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />









<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
  { content: attr(title);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>Preface</a></li>
<li class="chapter" data-level="1" data-path="section.html"><a href="section.html"><i class="fa fa-check"></i><b>1</b> 简介</a></li>
<li class="chapter" data-level="2" data-path="chapter-facet.html"><a href="chapter-facet.html"><i class="fa fa-check"></i><b>2</b> 不一样的分面</a><ul>
<li class="chapter" data-level="2.1" data-path="chapter-facet.html"><a href="chapter-facet.html#section-1"><i class="fa fa-check"></i><b>2.1</b> 背景数据</a></li>
<li class="chapter" data-level="2.2" data-path="chapter-facet.html"><a href="chapter-facet.html#section-2"><i class="fa fa-check"></i><b>2.2</b> 特定的分面</a></li>
<li class="chapter" data-level="2.3" data-path="chapter-facet.html"><a href="chapter-facet.html#extra-facet"><i class="fa fa-check"></i><b>2.3</b> 额外的分面</a></li>
<li class="chapter" data-level="2.4" data-path="chapter-facet.html"><a href="chapter-facet.html#section-3"><i class="fa fa-check"></i><b>2.4</b> 坐标轴截断</a></li>
<li class="chapter" data-level="2.5" data-path="chapter-facet.html"><a href="chapter-facet.html#section-4"><i class="fa fa-check"></i><b>2.5</b> 不想干的分面</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="chapter-summary.html"><a href="chapter-summary.html"><i class="fa fa-check"></i><b>3</b> 统计量</a></li>
<li class="appendix"><span><b>Appendix</b></span></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>References</a></li>
</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">ggplot2忍者秘笈</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="chapter-summary" class="section level1">
<h1><span class="header-section-number">Chapter 3</span> 统计量</h1>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)

pups &lt;-<span class="st"> </span>nlme<span class="op">::</span>RatPupWeight <span class="op">%&gt;%</span><span class="st"> </span>
<span class="st">    </span>janitor<span class="op">::</span><span class="kw">clean_names</span>() <span class="op">%&gt;%</span>
<span class="st">    </span>dplyr<span class="op">::</span><span class="kw">mutate</span>(<span class="dt">litter =</span> <span class="kw">as.integer</span>(litter))

p &lt;-<span class="st"> </span><span class="kw">ggplot</span>(pups, <span class="kw">aes</span>(<span class="dt">x =</span> litter, <span class="dt">y =</span> weight, <span class="dt">color =</span> treatment))</code></pre>
<p>针对某一个x值，我们可以画出相应y的统计量，比如均值、方差等，这些都可以“手工”的方式通过图层叠加来实现。</p>
<pre class="sourceCode r"><code class="sourceCode r">p <span class="op">+</span><span class="st"> </span><span class="kw">geom_jitter</span>(<span class="dt">shape=</span><span class="dv">1</span>) <span class="op">+</span><span class="st"> </span>
<span class="st">    </span><span class="kw">stat_summary</span>(<span class="dt">fun.y =</span> <span class="st">&quot;mean&quot;</span>, <span class="dt">size=</span><span class="dv">2</span>, <span class="dt">geom=</span><span class="st">&quot;point&quot;</span>, <span class="dt">color =</span> <span class="st">&#39;black&#39;</span>) <span class="op">+</span><span class="st"> </span>
<span class="st">    </span><span class="kw">stat_summary</span>(<span class="dt">fun.data =</span> mean_cl_normal, <span class="dt">geom=</span><span class="st">&quot;errorbar&quot;</span>, <span class="dt">width=</span>.<span class="dv">1</span>, <span class="dt">color=</span><span class="st">&#39;black&#39;</span>)</code></pre>
<p><img src="ggplot2-ninja_files/figure-html/unnamed-chunk-9-1.png" width="672" /></p>
<p>而图层不单单是画出几何图形而已，有一些是需要计算统计量的，最常见也最容易理解的箱式图图层<code>geom_boxplot</code>，需要计算四分位数，而有一些图层虽然看上去不需要做计算，比如显示误差的<code>geom_errorbar</code>，通常需要我们提供<code>ymin</code>和<code>ymax</code>，然而它按照我们提供的值去显示，没有涉及到图层内部的计算，但这只是默认行为而已，如果有留意<code>ggplot2</code>图层的默认参数的话，大概是会发现很多图层都有一个<code>stat = "identity"</code>的参数，这表示按照输入数据的值来画，不涉及计算，比如画柱状图的<code>geom_col</code>就是这样，它会按照用户输入的计算来画柱子的高度，而<code>geom_bar</code>的参数却不<code>stat = "count"</code>它则会去数数。所以这些图层都有<code>stat</code>参数，意味着我们可以改变默认行为的统计量来画图，比如说箱式图吧，箱体是25%, 50%, 70%的分位数来画的，假设我想用mean-sd, mean, mean+sd来画呢？当然可以。</p>
<pre class="sourceCode r"><code class="sourceCode r">f &lt;-<span class="st"> </span><span class="cf">function</span>(y) {
    r &lt;-<span class="st"> </span><span class="kw">quantile</span>(y, <span class="dt">probs =</span> <span class="kw">c</span>(<span class="fl">0.05</span>, <span class="fl">0.25</span>, <span class="fl">0.5</span>, <span class="fl">0.75</span>, <span class="fl">0.95</span>))
    r[<span class="dv">2</span>] &lt;-<span class="st"> </span><span class="kw">mean</span>(y) <span class="op">-</span><span class="st"> </span><span class="kw">sd</span>(y) 
    r[<span class="dv">3</span>] &lt;-<span class="st"> </span><span class="kw">mean</span>(y)
    r[<span class="dv">4</span>] &lt;-<span class="st"> </span><span class="kw">mean</span>(y) <span class="op">+</span><span class="st"> </span><span class="kw">sd</span>(y)
    <span class="kw">names</span>(r) &lt;-<span class="st"> </span><span class="kw">c</span>(<span class="st">&quot;ymin&quot;</span>, <span class="st">&quot;lower&quot;</span>, <span class="st">&quot;middle&quot;</span>, <span class="st">&quot;upper&quot;</span>, <span class="st">&quot;ymax&quot;</span>)
    r
}

p1 &lt;-<span class="st"> </span>p <span class="op">+</span><span class="st"> </span><span class="kw">stat_summary</span>(<span class="dt">fun.data=</span>f, <span class="dt">geom=</span><span class="st">&#39;boxplot&#39;</span>, <span class="dt">width=</span>.<span class="dv">8</span>) 
p2 &lt;-<span class="st"> </span>p <span class="op">+</span><span class="st"> </span><span class="kw">geom_boxplot</span>(<span class="kw">aes</span>(<span class="dt">group =</span> litter))
cowplot<span class="op">::</span><span class="kw">plot_grid</span>(p1, p2, 
    <span class="dt">labels =</span> <span class="kw">c</span>(<span class="st">&quot;mean +- sd&quot;</span>, <span class="st">&quot;quantile&quot;</span>))</code></pre>
<p><img src="ggplot2-ninja_files/figure-html/unnamed-chunk-10-1.png" width="960" /></p>
<p>这一招也是我在<a href="https://www.sciencedirect.com/science/article/pii/S1874391912007567">Proteomic investigation of the interactome of FMNL1 in hematopoietic cells unveils a role in calcium-dependent membrane plasticity</a>图一中把箱式图的中位数换成均值所使用的方法。</p>
<p>使用<code>stat_smooth(method = "lm", formula = y ~ 1)</code>，可以展示某一x取值区间的平均y值。</p>
<pre class="sourceCode r"><code class="sourceCode r">p <span class="op">+</span><span class="st"> </span><span class="kw">geom_point</span>() <span class="op">+</span><span class="st"> </span><span class="kw">stat_smooth</span>(<span class="dt">method =</span> <span class="st">&quot;lm&quot;</span>, <span class="dt">formula =</span> y <span class="op">~</span><span class="st"> </span><span class="dv">1</span>)</code></pre>
<p><img src="ggplot2-ninja_files/figure-html/unnamed-chunk-11-1.png" width="672" /></p>
<p>如果要计算中位数的话，可以使用<code>rlm</code>：</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(MASS)
p <span class="op">+</span><span class="st"> </span><span class="kw">geom_point</span>() <span class="op">+</span><span class="st"> </span><span class="kw">stat_smooth</span>(<span class="dt">method =</span> <span class="st">&quot;rlm&quot;</span>, <span class="dt">formula =</span> y <span class="op">~</span><span class="st"> </span><span class="dv">1</span>)</code></pre>
<p><img src="ggplot2-ninja_files/figure-html/unnamed-chunk-12-1.png" width="672" /></p>

</div>



</div>
            </section>

          </div>
        </div>
      </div>
<a href="chapter-facet.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="references.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"google": false,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"download": null,
"toc": {
"collapse": "subsection"
}
});
});
</script>

</body>

</html>
